package org.hefan.user.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hefan.basic.util.AjaxResult;
import org.hefan.basic.util.PageList;
import org.hefan.user.domain.Logininfo;
import org.hefan.user.query.LogininfoQuery;
import org.hefan.user.service.ILogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类
 */
@RestController
@RequestMapping("/logininfo")
@Api(tags = "登陆信息接口",description = "登陆信息接口详细描述")
public class LogininfoController {

    /**
     * 注入logininfoService
     */
    @Autowired
    private ILogininfoService logininfoService;

    /**
     * 查询全部
     * //处理请求：Get http://localhost:8080/logininfo
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询全部登陆信息",notes = "")
    public List<Logininfo> findAll() {
        return logininfoService.loadAll();
    }

    /**
     * 查询单个Logininfo
     * 处理请求：Get http://localhost:8080/logininfo/1
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个登陆信息",notes = "需要传入id")
    public Logininfo findOne(@PathVariable("id")Long id) {
        return logininfoService.loadById(id);
    }

    /**
     * 分页查询
     * post请求,参数在请求体中,通过@RequestBody获取请求体中的参数
     * @param logininfoQuery 分页查询参数
     * @return
     */
    @PostMapping
    @ApiOperation(value = "通过分页查询登陆信息",notes = "需要传入分页数据")
    public PageList<Logininfo> queryPage(@RequestBody LogininfoQuery logininfoQuery) {
        return logininfoService.queryPage(logininfoQuery);
    }

    /**
     * 删除登陆信息
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除登陆信息",notes = "通过id删除")
    public AjaxResult delete(@PathVariable("id")Long id) {
        try {
            logininfoService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }

    /**
     * 添加或修改登陆信息
     * @param logininfo
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或修改登陆信息",notes = "有id则为修改,没有id则是添加")
    public AjaxResult saveOrUpdate(@RequestBody Logininfo logininfo) {
        try {
            if (logininfo.getId() == null) {
                logininfoService.save(logininfo);
            } else {
                logininfoService.update(logininfo);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }

    /**
     * 批量删除登陆信息
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除登陆信息",notes = "传需要批量删除的登陆信息id")
    public AjaxResult batchRemove(@RequestBody List<Long> ids) {
        try {
            logininfoService.batchRemove(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }
}
